﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileForm.cs" company="HelloWorld">
//   BY苏宣
// </copyright>
// <summary>
//   Defines the FileForm type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Form
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Windows.Forms;

    using WindowsFormsApp2.Service;

    /// <summary>
    /// The file form.
    /// </summary>
    public partial class FileForm : Form
    {
        /// <summary>
        /// The file manager.
        /// </summary>
        private readonly FileManager fileManager;

        /// <summary>
        /// Initializes a new instance of the <see cref="FileForm"/> class.
        /// </summary>
        public FileForm()
        {
            this.InitializeComponent();
            this.fileManager = new FileManager();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        /// <summary>
        /// The form 1_ load.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Form1_Load(object sender, EventArgs e)
        {
            this.UpdateList();
            if (this.fileManager.Dirs.Count > 0)
            {
                this.file_list.SelectedIndex = 0;
            }

            // FileChange();
            this.drawer1.treeView1.DoubleClick += this.OpenFile;
            this.drawer1.treeView1.MouseClick += this.DeleteFile;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            
        }

        // 更新列表

        /// <summary>
        /// The update list.
        /// </summary>
        private void UpdateList()
        {
            this.file_list.Items.Clear();
            foreach (var dir in this.fileManager.Dirs)
            {
                this.file_list.Items.Add(dir);
            }
        }

        // 文件变更

        /// <summary>
        /// The file change.
        /// </summary>
        private void FileChange()
        {
            var dirs = Directory.EnumerateDirectories(@"./myresource/" + this.file_list.SelectedItem);
            foreach (var dir in dirs)
            {
                var files = Directory.EnumerateFiles(dir);
                var nodes = files.Select(
                        file => new TreeNode(file.Substring(file.LastIndexOf(@"\", StringComparison.Ordinal) + 1)))
                    .ToList();
                var dirName = dir.Substring(dir.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
                switch (dirName)
                {
                    case "公共":
                        this.drawer1.Nodes1 = nodes;
                        break;
                    case "笔记":
                        this.drawer1.Nodes2 = nodes;
                        break;
                    case "资源":
                        this.drawer1.Nodes3 = nodes;
                        break;
                }
            }

            this.drawer1.Change();
        }

        /// <summary>
        /// The open file.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OpenFile(object sender, EventArgs e)
        {
            if (this.drawer1.SelectedText == string.Empty)
            {
                return;
            }

            var fileName = this.drawer1.SelectedText;
            var partName = this.drawer1.GetPart();
            var dirName = this.file_list.SelectedItem.ToString();
            var path = $"./myresource/{dirName}/{partName}/{fileName}";
            Process.Start(Path.GetFullPath(path));
            this.drawer1.SelectedText = string.Empty;
        }

        /// <summary>
        /// The delete file.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void DeleteFile(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
            {
                return;
            }

            if (MessageBox.Show(@"是否删除该文件", @"确认消息", MessageBoxButtons.OKCancel) != DialogResult.OK)
            {
                return;
            }

            if (this.drawer1.SelectedText == string.Empty)
            {
                return;
            }

            var fileName = this.drawer1.SelectedText;
            var partName = this.drawer1.GetPart();
            var dirName = this.file_list.SelectedItem.ToString();
            var path = $"./myresource/{dirName}/{partName}/{fileName}";
            File.Delete(Path.GetFullPath(path));
            this.drawer1.SelectedText = string.Empty;
            this.FileChange();
        }

        /// <summary>
        /// The create_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Create_Click(object sender, EventArgs e)
        {
            var dir = this.textBox1.Text;
            this.textBox1.Clear();
            if (dir == String.Empty)
            {
                MessageBox.Show(@"文件夹名不能为空");
                return;
            }

            if (this.fileManager.CreateDir(dir))
            {
                this.UpdateList();
                MessageBox.Show(@"创建成功");
            }
            else
            {
                MessageBox.Show(@"重名，创建失败");
            }
        }

        /// <summary>
        /// The list box 1_ selected index changed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.FileChange();
        }

        /// <summary>
        /// The form 1_ drag enter.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.All : DragDropEffects.None;
        }

        /// <summary>
        /// The form 1_ drag drop.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            var path = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();       // 获得路径
            var fileName = path.Substring(path.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
            var partName = this.drawer1.GetPart();
            if (partName == string.Empty)
            {
                partName = "资源";
            }

            var dirName = this.file_list.SelectedItem.ToString();
            var deskPath = $"./myresource/{dirName}/{partName}/{fileName}";
            File.Move(path, Path.GetFullPath(deskPath));
            this.FileChange();
        }

        /// <summary>
        /// The file_list_ mouse down.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void File_list_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
            {
                return;
            }

            var dirName = this.file_list.SelectedItem.ToString();
            if (MessageBox.Show($@"是否删除文件夹:{dirName}", @"确认消息", MessageBoxButtons.OKCancel) != DialogResult.OK)
            {
                return;
            }

            var path = $"./myresource/{dirName}";
            Directory.Delete(Path.GetFullPath(path), true);
            this.file_list.Items.Remove(dirName);
            this.fileManager.Dirs.Remove(dirName);
            this.FileChange();
            this.UpdateList();
        }

        /// <summary>
        /// The explorer_button_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Explorer_btn_Click(object sender, EventArgs e)
        {
            var path = @"./myresource";
            path += "/" + this.file_list.SelectedItem;
            Process.Start("explorer.exe", Path.GetFullPath(path));
        }

        /// <summary>
        /// The note_button_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Note_btn_Click(object sender, EventArgs e)
        {
            var note = new Note(Path.GetFullPath(@"./myresource/" + this.file_list.SelectedItem));
            if (note.ShowDialog() == DialogResult.OK)
            {
                this.FileChange();
            }
        }

        /// <summary>
        /// The button 2_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button2_Click(object sender, EventArgs e)
        {
            const string PathStr = @"./myresource";
            var networker = new Networker(Path.GetFullPath(PathStr));
            var files = this.drawer1.Nodes1.Select(node => node.Text).ToList();
            networker.CheckList((string)this.file_list.SelectedItem, files);
            this.FileChange();
            MessageBox.Show(@"已同步到最新");
        }
    }
}
